#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/21 22:08
# @Author  : when
# @File    : boss_parser.py
# @Description : boss网页解析器
import traceback

from loguru import logger
from bs4 import BeautifulSoup
from bs4.element import Tag

from crawler.orm_model.page import Page
from crawler.orm_model.page import PageTypeChoice
from crawler.orm_model.boss_job import BossJob
from crawler.orm_model.page_boss_job import PageBossJob
from crawler.website.boss.boss_utils import gen_job_flag
from crawler.website.boss.boss_utils import card_to_boss
from components.mysql_client.sqlalchemy_connector import SqlConnector


class BossParser:
    def __init__(self, db: SqlConnector, page_obj: Page):
        self.db = db
        self.page = page_obj

    def parser_job_card(self):
        """
        解析搜索的工作列表html的内容，获取有用信息
        :return:
        """
        try:
            log_prefix = f"Page({self.page.id}): "
            # 由于boss页面结构不固定，不选用xpath方式解析
            btf_soup = BeautifulSoup(self.page.html, 'html.parser')
            job_card_wrapper = btf_soup.select(".job-card-wrapper")
            for job_card in job_card_wrapper:
                job_card: Tag
                boss_job = BossJob()
                boss_job, job_card = card_to_boss(boss_job, job_card)
                boss_job.flag = gen_job_flag(boss_job)
                # 判断数据是否重复
                if self.db.query(BossJob).filter_by(flag=boss_job.flag).count():
                    logger.warning(f"{log_prefix} 当前数据与数据库中[flag={boss_job.flag}]存在重复，暂不入库")
                    continue
                # 获取需要爬取的页面，入库page表
                details_url = job_card.select_one(".job-card-left")["href"]
                if not details_url:
                    continue
                details_url = f"https://www.zhipin.com{details_url}"
                page_obj = Page(url=details_url, type=PageTypeChoice.BOSS_JOB_DETAILS)
                try:
                    self.db.insert_one(page_obj, auto_commit=False)
                    self.db.insert_one(boss_job, auto_commit=False)
                    page_boss_job = PageBossJob(page_id=page_obj.id, boss_job_id=boss_job.id)
                    self.db.insert_one(page_boss_job)
                except Exception as e:
                    logger.error(f"{log_prefix} 数据入库异常，立即回滚 error: {e}")
                    self.db.rollback()
                    return False
        except Exception as e:
            logger.error(f"{log_prefix} parser job-card error: {e}, {traceback.format_exc()}")
            return False
        return True
